//
//  MapNearLocationViewController.m
//  TMapDemo
//
//  Created by ios on 14-10-14.
//
//

#import "MapNearLocationViewController.h"
#import "MapNavigationViewController.h"

@interface MapNearLocationViewController ()
@property (nonatomic, strong) IBOutlet UILabel *titleLabel;
@property (nonatomic, strong) IBOutlet UILabel *subTitleLabel;
@property (nonatomic, strong) IBOutlet UILabel *telLabel;

@property (nonatomic, strong) IBOutlet UIView *infoView;
@property (nonatomic, strong) IBOutlet TMapView *_mapView;

@end

@implementation MapNearLocationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)backMethod
{
    if (self._mapView != nil) {
        [self._mapView StopGetPosition];
    }
    self.annotation = nil;
    self.location = nil;
    self._mapView.delegate = self;
    self._mapView = nil;
    
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIButton *busBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [UIImage imageNamed:@"route_nav"].size.width, [UIImage imageNamed:@"route_nav"].size.height)];
    [busBtn setBackgroundImage:[UIImage imageNamed:@"route_nav"] forState:UIControlStateNormal];
    [busBtn addTarget:self action:@selector(mapNavigation) forControlEvents: UIControlEventTouchUpInside];
    UIBarButtonItem* bus = [[UIBarButtonItem alloc] initWithCustomView:busBtn];
    self.navigationItem.rightBarButtonItem = bus;
    
    self.title = @"详情";
    self._mapView.delegate = self;
    self._mapView.mapScale = 14;
    self._mapView.ShowPosition = YES;
    // 当前跟踪模式,跟踪状态
    self._mapView.UserTrackMode = TUserTrackingModeNone;

    self.titleLabel.text = self.annotation.title;
    self.subTitleLabel.text = self.annotation.subtitle;
    self.telLabel.text = self.annotation.strtel;
    
    [self.infoView setFrame:CGRectMake(0, 0, 320, 120)];
    [self._mapView setFrame:CGRectMake(0, 120 , 320, CGRectGetHeight(self.view.frame) - 120)];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self._mapView addAnnotation:self.annotation];
    [self._mapView selectAnnotation:self.annotation animated:YES];
    [self._mapView setCenterCoordinate:self.annotation.coordinate animated:NO];
    self._mapView.ShowPosition = YES;
    [self._mapView StartGetPosition];

    //    NSLog(@"self.item.coordinate latitude = %f",self.annotation.coordinate.latitude);
    //    NSLog(@"self.item.coordinate longitude = %f",self.annotation.coordinate.longitude);
    
}


- (void)mapNavigation
{
    MapNavigationViewController *vc = [MapNavigationViewController new];
    vc.location = self.location;
    vc.annotation = self.annotation;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)mapView:(TMapView *)mapView didUpdateUserLocation:(TUserLocation *)userLocation;
{
//    self.location = userLocation.location;
    [self._mapView StopGetPosition];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
